% TEST_ID: LANG-EXPAND-0054
% CATEGORY: language/reflection
% 测试 evalin 函数

function test_evalin
    evalin('base', 'x = 3;');
    evalin('base', 'y = 7;');
    test_evalin_base()
    test_evalin_caller()
    test_evalin_inside_eval()
end



function test_evalin_base()
    x = 99999;
    y = 99999;
    evalin('base', 'assert(x+y == 10);');
    evalin('base', 'x=x+10;');
    evalin('base', 'assert(x+y == 20);');
    assert(evalin('base', 'x+y') == 20);
end

function test_evalin_caller()
    x = 103;
    y = 107;
    function inner_func()
        z = 0;
        evalin('caller', 'assert(x+y == 210);');
        evalin('caller', 'x=x+10;');
        evalin('caller', 'assert(x+y == 220);');
    end
    inner_func()
end

function test_evalin_inside_eval()
    k = 1;
    function inner_func1()
        z = 1;
    end
    inner_func1()
    function inner_func2()
        % eval -> evalin -> eval
        eval('evalin(''caller'', ''eval(''''k = 3;'''');'')');
    end
    inner_func2()
    assert(k == 3)
    function inner_func3()
        % eval -> eval -> evalin
        eval('eval(''evalin(''''caller'''', ''''k = 5;'''');'')');
    end
    inner_func3()
    assert(k == 5)
    function inner_func4()
        % evalin -> eval -> eval
        evalin('caller', 'eval(''eval(''''k = 7;'''');'');');
    end
    inner_func4()
    assert(k == 7)
end

